package com.smartwebx.fr.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.weaver.patterns.ParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.smartwebx.fr.bean.mysql.Page;
import com.smartwebx.fr.bean.mysql.PageFreetable;
import com.smartwebx.fr.service.ckeditor.PageService;
import com.smartwebx.fr.util.CurrLocalSeIdUseParam;
import com.smartwebx.fr.util.StationFinalFields;

@Controller
@RequestMapping("/fastTemplate")
public class FastTemplateController {

	@Autowired
	private PageService pageService;

	@RequestMapping("/savePageTitle")
	@ResponseBody
	public void savePageTitle(Integer pageId, String pageTitle,
			Boolean remberParam) {
		pageService.savePageName(pageId, remberParam, pageTitle);
	}

	@RequestMapping("/removeSessionId")
	@ResponseBody
	public void removeSessionId(String sessionId) {
		CurrLocalSeIdUseParam.removePageUseArgsBySessionId(sessionId);
	}

	@RequestMapping("/saveReport")
	@ResponseBody
	public void saveReport(Integer pageId, String data, Integer ckWidth,
			Integer ckHeight, String hotTabIds, String hotTabDatas,
			String freeTabHtmls, String pointIdStrs) throws IOException {
		List<PageFreetable> pageFreeTables = Lists.newArrayList();

		if (!Objects.equal(hotTabIds, "")) {
			Iterable<String> hotTabIdIter = Splitter.on('@').split(hotTabIds);
			Iterable<String> hotTabDataIter = Splitter.on('@').split(
					hotTabDatas);
			Iterable<String> freeTabHtmlsIter = Splitter.on('@').split(
					freeTabHtmls);

			int i = 0;
			for (String hotTabData : hotTabDataIter) {
				String hotTabId = Iterables.get(hotTabIdIter, i);
				hotTabData = URLDecoder.decode(hotTabData,
						StationFinalFields.DEFAULT_ENCODE);
				String freeTabHtml = URLDecoder.decode(
						Iterables.get(freeTabHtmlsIter, i),
						StationFinalFields.DEFAULT_ENCODE);

				PageFreetable pageFreeTab = new PageFreetable();
				pageFreeTab.setId(hotTabId);
				pageFreeTab.setHotData(hotTabData);
				pageFreeTab.setPageId(pageId);
				pageFreeTab.setFreeTabHtml(freeTabHtml);

				pageFreeTables.add(pageFreeTab);
				i++;
			}
		}
		Iterable<String> pointIdIter = Splitter.on(',').split(pointIdStrs);
		Set<String> pointIdSet = Sets.newHashSet(pointIdIter);
		pageService.saveReport(pageId, data, ckWidth, ckHeight, pageFreeTables,
				pointIdSet);
	}

	/**
	 * 显示文本，判断 pageId 是否存在，不存在则新增一条数据
	 * 
	 * @param pageId
	 * @param userType
	 * @return
	 * @throws ParserException
	 * @throws IOException
	 */
	@RequestMapping
	public Object getIndex(HttpServletRequest request, Integer pageId) {
		Map<String, Object> root = new HashMap<String, Object>();
		try {
			String currentSessionId = UUID.randomUUID().toString();
			if (!pageService.checkPageIdExist(pageId)) {
				// 新增
				Page page = new Page();
				page.setPageId(pageId);
				page.setCkHeight(720);
				page.setPageTitle("默认标题");
				pageService.savePage(page);

				root.put("pageText", "");
				root.put("pageId", pageId);
				root.put("ckHeight", page.getCkHeight());
				root.put("pageTitle", page.getPageTitle());
			} else {
				Page pageBean = pageService.getPageByPageId(
						request.getRequestURL(), pageId);

				Integer ckHeight = pageBean.getCkHeight();
				String pageText = MoreObjects.firstNonNull(
						pageBean.getPageText(), "");

				root.put("pageTitle", pageBean.getPageTitle());
				root.put("pageText", pageText);
				root.put("pageId", pageId);
				root.put("ckHeight", ckHeight);
			}
			root.put("currentSessionId", currentSessionId);
		} catch (Exception e) {
			e.printStackTrace();
			root.put("pageText", "程序错误。。。。。。");
			root.put("ckHeight", 720);
		}
		return root;
	}
}
